import 'package:aio/aio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/bean/http_data.dart';
import 'package:app/src/http/bean/wroks/address_bean.dart';
import 'package:app/src/http/bean/wroks/contract_bean.dart';
import 'package:app/src/ui/route/helper/route_helper.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_card.dart';

import '../../../../../../http/bean/worksdo/material_requisition_bean.dart';
import '../../../../../../http/bean/wroks/dispatchingaudit_bean.dart';
import '../../../../../../http/model/config_model.dart';
import '../../../../../dialog/dailog.dart';

///请购单信息
class UiMaterial {
  ///data 信息
  ///oldData 历史信息
  ///isPurStartDate 是否显示请购日期
  static Widget build(BuildContext context, MaterialInfo? data,
      MaterialInfo? oldData, StateSetter setState,
      {String? title, bool isPurStartDate = true}) {
    List<Widget> children = [];
    if (StringUtils.isEmpty(title)) {
      title = AppStrings.of(context).PurchaseOrderInformation; //请购单信息
    }
    children.add(UiBase.title(context, title)); //标题
    children.add(UiBase.item(
        context,
        AppStrings.of(context).PurchaseOrderNumber,
        data?.PurCode,
        oldData?.PurCode,
        isAllowEmpty: true,
        defaultValue: AppStrings.of(context).Automaticgeneration,
        isGrey: true)); //请购单号
    children.add(UiBase.item(context, AppStrings.of(context).FactoryName3,
        data?.FactoryName, oldData?.FactoryName,
        isBlue: true, isAllowEmpty: true, onTap: () {
      ConfigModel.getFactorygetlist(context, true, (value) {
        if (value != null && value!.isSuccessForData()) {
          var facory = data?.getFactory();
          AppDialog.showDialogWithRollerPicker(context,
                  datas: value.Data, data: facory)
              ?.then((value) {
            //print("===========工厂$value");
            if (value != null) {
              setState?.call(() {
                data?.FactoryName = value?.FactoryName;
                data?.FactoryId = value?.FactoryId;
              });
            }
          });
        }
      });
    })); //工厂
    if (isPurStartDate || true) {
      //fixme 请购日期始终显示,和测试商量过的。
      children.add(UiBase.item(
        context,
        AppStrings.of(context).DateRequest,
        data?.getPurStartDate(),
        oldData?.getPurStartDate(),
        isAllowEmpty: true,
        isGrey: true,
        isStart: true,
      )); //请购日期
    }
    children.add(UiBase.item(context, AppStrings.of(context).PurchaseDate3,
        data?.getPurEndDate(), oldData?.getPurEndDate(),
        isAllowEmpty: true,
        isBlue: true,
        isStart: true,
        defaultValue: AppStrings.of(context).PurchaseDate4, onTap: () {
      AppDialog.showDialogWithDatePicker(
              context: context, initialDate: data?.PurEndDateTime)
          ?.then((value) {
        if (value != null) {
          setState?.call(() {
            String dateTime = DateTimeUtils.getDateTimeFormat(
                dateTime: value, dateAndTime: false); //年，月，日
            //print("日期：${dateTime}\t$value");
            data?.PurEndDate = dateTime;
            data?.PurEndDateTime = value;
          });
        }
      });
    })); //要货日期
    children.add(UiBase.item(context, AppStrings.of(context).Bindornot,
        data?.getBind(context), oldData?.getBind(context),
        isAllowEmpty: true, isBlue: true, onTap: () {
      var datas = <RollerBean>[];
      datas.add(RollerBean(key: "1", value: AppStrings.of(context).Yes));
      datas.add(RollerBean(key: "0", value: AppStrings.of(context).No));
      AppDialog.showDialogWithRollerPicker(context,
              datas: datas, value: data?.getBind(context))
          ?.then((value) {
        if (value != null && value!.value != null) {
          setState?.call(() {
            if (value.key == "1") {
              data?.IsBind = true;
            } else {
              data?.IsBind = false;
            }
          });
        }
      });
    })); //是否绑定
    children.add(UiBase.item(context, AppStrings.of(context).ProjectName2,
        data?.ProjectName, oldData?.ProjectName,
        isAllowEmpty: true,
        isBlue: true,
        defaultValue: AppStrings.of(context).ManufacturerName3, onTap: () {
      PreSearchMaterialInfo searchData = PreSearchMaterialInfo();
      searchData.type = PreSearchMaterialInfo.Type_ProjectName;
      if (data != null &&
          data.PurDemandList != null &&
          data.PurDemandList!.length > 0) {
        searchData.MaterialType = data?.PurDemandList![0].MaterialType;
      }
      AppRouteHelper.goSearchProjiectNameRoute(context, searchData, (value) {
        setState?.call(() {
          //fixme 需要对接接口
          if (value is OtherWorkApply) {
            data?.ProjectName = value.Name;
            data?.ProjectId = value.Id;
          }
        });
      });
    })); //项目
    children.add(UiBase.itemWithEdit(context, AppStrings.of(context).ItemIntro,
        data?.BeiZhu, oldData?.BeiZhu,
        isAllowEmpty: true, onChanged: (value) {
      data?.BeiZhu = value;
    })); //备注
    var column = UiBase.column(context, children);
    var card = UiCard.build(context, column);
    return card;
  }
}
